package leecode.q27;

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums.length == 0) {
            return 0;
        }
        int left = 0;
        int right = nums.length - 1;
        int length = 0;
        while (left != right) {
            if (nums[left] != val) {
                left++;
                length++;
            } else {
                if (nums[right] == val) {
                    right--;
                } else {
                    swapNum(left, right, nums);
                    right--;
                }
            }

        }
        if (nums[left] != val) {
            length++;
        }
        return length;
    }

    private void swapNum(int left, int right, int[] nums) {
        int temp = nums[right];
        nums[right] = nums[left];
        nums[left] = temp;
    }
}
